﻿using ZYCommon;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml;

namespace TextEditor
{
    

    public class ImageLineAction : ImageEditAction
    {
        public bool Arrow = false;
        public System.Drawing.Color Color = System.Drawing.Color.Black;
        public Point p1;
        public Point p2;
        public int Width = 1;

        public override bool DesignChangeBounds(Rectangle NewBounds)
        {
            Rectangle bounds = this.Bounds;
            this.p1 = RectangleObject.GetAcmePos(NewBounds, RectangleObject.GetAcmeIndex(bounds, this.p1));
            this.p2 = RectangleObject.GetAcmePos(NewBounds, RectangleObject.GetAcmeIndex(bounds, this.p2));
            return true;
        }

        public override bool Execute(Graphics g, Rectangle ClipRect)
        {
            using (Pen pen = new Pen(this.Color, (float) this.Width))
            {
                if (this.Arrow)
                {
                    using (AdjustableArrowCap cap = new AdjustableArrowCap(4f, 4f))
                    {
                        pen.CustomEndCap = cap;
                        g.DrawLine(pen, this.p1, this.p2);
                    }
                }
                else
                {
                    g.DrawLine(pen, this.p1, this.p2);
                }
            }
            return true;
        }

        public override bool FromXML(XmlElement myElement)
        {
            this.p1 = new Point(Convert.ToInt32(myElement.GetAttribute("x1")), Convert.ToInt32(myElement.GetAttribute("y1")));
            this.p2 = new Point(Convert.ToInt32(myElement.GetAttribute("x2")), Convert.ToInt32(myElement.GetAttribute("y2")));
            this.Width = Convert.ToInt32(myElement.GetAttribute("width"));
            this.Color = StringCommon.ColorFromHtml(myElement.GetAttribute("color"), System.Drawing.Color.Black);
            this.Arrow = myElement.GetAttribute("arrow") == "1";
            return true;
        }

        public override bool IsVisible(Rectangle ClipRect)
        {
            return RectangleObject.GetRectangle(this.p1, this.p2).IntersectsWith(ClipRect);
        }

        public override bool ToXML(XmlElement myElement)
        {
            myElement.SetAttribute("x1", this.p1.X.ToString());
            myElement.SetAttribute("y1", this.p1.Y.ToString());
            myElement.SetAttribute("x2", this.p2.X.ToString());
            myElement.SetAttribute("y2", this.p2.Y.ToString());
            myElement.SetAttribute("width", this.Width.ToString());
            myElement.SetAttribute("color", StringCommon.ColorToHtml(this.Color));
            myElement.SetAttribute("arrow", this.Arrow ? "1" : "0");
            return true;
        }

        public override string ActionName
        {
            get
            {
                return "line";
            }
        }

        public override Rectangle Bounds
        {
            get
            {
                return RectangleObject.GetRectangle(this.p1, this.p2);
            }
            set
            {
            }
        }
    }
}

